package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.oocl.dao.GoodsDao;
import com.oocl.pojo.Goods;
import com.oocl.pojo.Warehouse;
import com.oocl.util.DBUtil;

public class GoodsDaoImpl implements GoodsDao{
	@Override
	public int addGoods(Goods g) {
		String sql = "insert into goods(id, name, price, count, pd, bid) values (?,?,?,?,?,?)";
		
		PreparedStatement pst =null;
		Connection con = null;
		int m = 0; 
		
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1,g.getId());
			pst.setString(2, g.getName());
			pst.setDouble(3, g.getPrice());
			pst.setInt(4, g.getCount());
			pst.setDate(5, new java.sql.Date(g.getPd().getTime()));
			pst.setInt(6, g.getBid());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, null);
		}
		return m;	
	}

	@Override
	public List<Goods> getGoodsFromWareHouse(int bid) {
		List<Goods> list = new ArrayList<Goods>();
		String sql = "select * from goods where bid=?";
		PreparedStatement pst = null;
		ResultSet rs = null;
		Connection con = DBUtil.createConnection();
		Goods c = null;
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, bid);
			rs = pst.executeQuery();
			
			while(rs.next()){
				c = new Goods();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setPrice(rs.getDouble("price"));
				c.setCount(rs.getInt("count"));
				c.setPd(new Date(rs.getDate("pd").getTime()));
				c.setBid(bid);
				list.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, rs);
		}
		return list;
	}

	@Override
	public int deleteGoods(String id) {
		String sql = "delete from goods where id=?";
		
		PreparedStatement pst =null;
		Connection con = null;
		int m = 0; 
		
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, id);
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, null);
		}
		return m;	
	}

	@Override
	public int updateGoods(Goods good) {
		String sql = "update goods set name=?,price=?,count=?,pd=?,bid=? where id=?";
		
		PreparedStatement pst =null;
		Connection con = null;
		int m = 0; 
		System.out.println(" in update.........");
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, good.getName());
			pst.setDouble(2, good.getPrice());
			pst.setInt(3, good.getCount());
			pst.setDate(4, new java.sql.Date(good.getPd().getTime()));
			pst.setInt(5, good.getBid());
			pst.setString(6, good.getId());
			
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, null);
		}
		return m;	
	}

	@Override
	public Goods loadGoods(String id) {
		String sql = "select * from goods where id=?";
		PreparedStatement pst = null;
		ResultSet rs = null;
		Connection con = DBUtil.createConnection();
		Goods c = null;
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, id);
			rs = pst.executeQuery();
			
			if(rs.next()){
				c = new Goods();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setPrice(rs.getDouble("price"));
				c.setCount(rs.getInt("count"));
				c.setPd(new Date(rs.getDate("pd").getTime()));
				c.setBid(rs.getInt("bid"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, rs);
		}
		return c;
	}
	
}
